using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using DanielSig.UnityExtensions;
using DanielSig;
using System.Linq;

[CustomPropertyDrawer(typeof(PropertyAssignment))]
//[CanEditMultipleObjects]
public class PropertyAssignmentInspector : PropertyDrawer 
{
	public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
	{
		EditorGUI.BeginProperty(position, label, property);
		
		Rect typeRect = new Rect (position.x + 30, position.y, 96, position.height);
		Rect nameRect = new Rect (position.x + 140, position.y, 150, position.height);
		
		String nameString = property.FindPropertyRelative("PropertyName").stringValue;
		
		label.text = nameString.Length > 0 ? " " : "                            ____________________";//property.FindPropertyRelative("PropertyName").stringValue;
		position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
		
		
		int indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;
		
		Rect valueRect = new Rect (position.x, position.y, 200, position.height);
		
		SerializedProperty name = property.FindPropertyRelative("PropertyName");
		name.stringValue = EditorGUI.TextField(nameRect, name.stringValue);
		
		
		PropertyType type = (PropertyType)property.FindPropertyRelative("Type").enumValueIndex;
		property.FindPropertyRelative("Type").enumValueIndex = (int)(PropertyType)EditorGUI.EnumPopup(typeRect, type);
		
		SerializedProperty prop;
		
		switch(type)
		{
			case PropertyType.Float:
				EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("FloatValue"), GUIContent.none);
				break;
			case PropertyType.Int:
				EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("IntValue"), GUIContent.none);
				break;
			case PropertyType.String:
				EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("StringValue"), GUIContent.none);
				break;
			case PropertyType.Vector2:
				valueRect.y -= position.height;
				prop = property.FindPropertyRelative("Vector2Value");
				prop.vector2Value = EditorGUI.Vector2Field(valueRect, "", prop.vector2Value);
				break;
			case PropertyType.Vector3:
				valueRect.y -= position.height;
				prop = property.FindPropertyRelative("Vector3Value");
				prop.vector3Value = EditorGUI.Vector3Field(valueRect, "", prop.vector3Value);
				break;
			case PropertyType.Quaternion:
				valueRect.y -= position.height;
				prop = property.FindPropertyRelative("QuaternionValue");
				prop.quaternionValue = Quaternion.Euler(EditorGUI.Vector3Field(valueRect, "", prop.quaternionValue.eulerAngles));
				break;
			case PropertyType.GameObject:
				EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("GameObjectValue"), GUIContent.none);
				break;
			case PropertyType.Component:
				EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("ComponentValue"), GUIContent.none);
				break;
			case PropertyType.Boolean:
				EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("BooleanValue"), GUIContent.none);
				break;
			default:
				break;
		}
		
		EditorGUI.indentLevel = indent;
		EditorGUI.EndProperty();
	}
}